<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部职别" prop="identifier">
        <el-input
          v-model="queryParams.identifier"
          placeholder="请输入部职别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="birth">
        <el-input
          v-model="queryParams.birth"
          placeholder="请输入出生日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位部门" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入单位部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="userCategory">
        <el-input
          v-model="queryParams.userCategory"
          placeholder="请输入类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="未还数量" prop="outstandingNumber">
        <el-input
          v-model="queryParams.outstandingNumber"
          placeholder="请输入未还数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计借阅次数" prop="accumulatedBorrow">
        <el-input
          v-model="queryParams.accumulatedBorrow"
          placeholder="请输入累计借阅次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借阅数量限制" prop="numberLimit">
        <el-input
          v-model="queryParams.numberLimit"
          placeholder="请输入借阅数量限制"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新者" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入更新者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker clearable
                        v-model="queryParams.updateTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:baseinfoh:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:baseinfoh:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:baseinfoh:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:baseinfoh:import']"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:baseinfoh:export']"
        >导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :ixdex="indexMethod" />
      <el-table-column label="姓名" align="center" prop="username" />
      <el-table-column label="部职别" align="center" prop="identifier" />
      <el-table-column label="出生日期" align="center" prop="birth" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="单位部门" align="center" prop="department" />
      <el-table-column label="身份证号" align="center" prop="idNumber" />
      <el-table-column label="联系方式" align="center" prop="contacttype" />

    <!--  <el-table-column label="未还数量" align="center" prop="outstandingNumber" />
      <el-table-column label="累计借阅次数" align="center" prop="accumulatedBorrow" />
      <el-table-column label="借阅数量限制" align="center" prop="numberLimit" />-->
     <!-- <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="部门ID" align="center" prop="deptId" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleDelete(scope.row)"

          >删除</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handlelook(scope.row)"

          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" custom-class="gaodu">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改新基本信息对话框 -->

    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form label-position="right" ref="form" :model="form"  :rules="rules"  label-width="150px">
        <el-container>
          <el-header>
            <el-divider content-position="left">
              <p style="color: #00afff; font-size: 18px;">读者信息</p>
            </el-divider>
          </el-header>
          <el-main>
            <el-col :span="12" >
              <el-form-item label="姓名" prop="username">
<!--                <el-autocomplete-->
<!--                  style="width: 100%"-->
<!--                  placeholder="请输入姓名"-->
<!--                  v-model="form.username"-->
<!--                  :fetch-suggestions="querySearchAsync"-->
<!--                  @select="handleSelect"-->
<!--                >-->
<!--                </el-autocomplete>-->
                <el-input v-model="form.username" placeholder="请输入姓名" style="width: 100%"/>
              </el-form-item>

              <el-form-item label="身份证号" prop="idNumber">
                <el-input v-model="form.idNumber" placeholder="请输入身份证号" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="部职别" prop="identifier">
                <el-input v-model="form.identifier" placeholder="请输入部职别" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="联系方式" prop="contacttype">
                <el-input v-model="form.contacttype" placeholder="请输入联系方式" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="出生日期" prop="birth">
                <el-input v-model="form.birth" placeholder="请输入出生日期" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="性别" prop="gender">
                <el-input v-model="form.gender" placeholder="请输入性别" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="部门" prop="department">
                <el-input v-model="form.department" placeholder="请输入部门" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="身份证号" prop="idNumber">
                <el-input v-model="form.idNumber" placeholder="请输入身份证号" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-main>







            <!-- <el-col :span="12" >
             <el-form-item label="归还日期" prop="givebackDate">


               <el-date-picker clearable
                               v-model="form.givebackDate"
                               type="date"
                               value-format="yyyy-MM-dd"
                               placeholder="请输入归还日期"
                               style="width: 100%">
               </el-date-picker>

             </el-form-item>
             </el-col>-->

          <el-header>
            <el-divider content-position="left">
              <p style="color: #00afff; font-size: 18px;">备注</p>
            </el-divider>
          </el-header>
            <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" style="width: 80%"/>
            </el-form-item>


        </el-container>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>


  </div>
</template>

<script>
  import { listBaseinfo, getBaseinfo, delBaseinfo, addBaseinfo, updateBaseinfo } from "@/api/group/sb/baseinfo";
  import { getToken } from "@/utils/auth";
  export default {
    name: "Baseinfo",
    data() {
      return {
        // 遮罩层
        loading: true,
        //时间
        searchData: {
          inputValue1: '',
          inputValue2: '',
          date: ''
        },
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 新基本信息表格数据
        baseinfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          username: null,
          identifier: null,
          birth: null,
          gender: null,
          age: null,
          department: null,
          idNumber: null,
          contacttype: null,
          userCategory: null,
          outstandingNumber: null,
          accumulatedBorrow: null,
          numberLimit: null,
          userId: null,
          deptId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          note1: null,
          note2: null,
          note3: null,
          note4: null,
          note5: null,
          note6: null,
          note7: null,
          note8: null,
          note9: null,
          note10: null
        },
          upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: {Authorization: "Bearer " + getToken()},
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/system/baseinfoh/importData",
          fileList: []
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    mounted() {
      this.getNowTime()
    },
    created() {
      this.getList();
    },
    methods: {
      //时间
      getFilterData() {
        this.$emit('getFilterData', this.searchData)
      },
      getNowTime() {
        var now = new Date()
        var year = now.getFullYear() // 得到年份
        var month = now.getMonth() // 得到月份
        var date = now.getDate() // 得到日期
        month = month + 1
        month = month.toString().padStart(2, '0')
        date = date.toString().padStart(2, '0')
        var defaultDate = `${year}-${month}-${date}`
        this.$set(this.searchData, 'date', defaultDate)
      },
      //生成表头序号
      indexMethod(index){
        return index+1;
      },
      /** 查询新基本信息列表 */
      getList() {
        this.loading = true;
        listBaseinfo(this.queryParams).then(response => {
          this.baseinfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },



      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          username: null,
          identifier: null,
          birth: null,
          gender: null,
          age: null,
          department: null,
          idNumber: null,
          contacttype: null,
          userCategory: null,
          outstandingNumber: null,
          accumulatedBorrow: null,
          numberLimit: null,
          userId: null,
          deptId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          note1: null,
          note2: null,
          note3: null,
          note4: null,
          note5: null,
          note6: null,
          note7: null,
          note8: null,
          note9: null,
          note10: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加基本信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getBaseinfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改基本信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateBaseinfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBaseinfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 详情操作 */
      handlelook(row) {
        const id = row.id;
        // const tableName = row.tableName || this.tableNames[0];
        const params = {pageNum: this.queryParams.pageNum};

        this.$router.push({path: "/basedata/reader_detail", query: {id}});
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除新基本信息编号为"' + ids + '"的数据项？').then(function() {
          return delBaseinfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('/system/baseinfoh/importTemplate', {}, `读者信息导入表.xlsx`)
      },

      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "导入";
        this.upload.open = true;
      },
// 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
// 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
        this.getList();
      },
// 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/baseinfoh/export', {
          ...this.queryParams
        }, `读者信息表_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
